package uk.co.revisual.domino.proxies
{
	import flexunit.framework.Assert;
	
	import org.puremvc.as3.multicore.utilities.statemachine.State;
	
	import uk.co.revisual.domino.core.FSMHistory;
	
	public class FSMHistoryProxyTest
	{		
		
		private var testSubject:FSMHistory;
		private var history:FSMHistoryProxy;
		
		[Before]
		public function setUp():void
		{
			history = new FSMHistoryProxy();
			history.setTransition( "action001" );
			history.confirmTransitionToNewState( new State("state001") );
			history.setTransition( "action002" );
			history.confirmTransitionToNewState( new State("state002") );
			history.setTransition( "action003" );
			history.confirmTransitionToNewState( new State("state003") );
			history.setTransition( "action004" );
			history.confirmTransitionToNewState( new State("state004") );
			history.setTransition( "action005" );
			history.confirmTransitionToNewState( new State("state005") );
			history.setTransition( "action006" );
			history.confirmTransitionToNewState( new State("state006") );
			testSubject = history;
		}
		
		[After]
		public function tearDown():void
		{
			testSubject = null;
			history = null
		}
		
		[Test]
		public function testGet_currentReferringAction():void
		{
			Assert.assertEquals( testSubject.currentReferringAction, "action006");
		}
		
		[Test]
		public function testGet_currentState():void
		{
			Assert.assertEquals( testSubject.currentState.name, "state006" );
		}
		
		[Test]
		public function testGetStateAt():void
		{
			Assert.assertNull( testSubject.getStateAt( -1 ) );
			Assert.assertEquals( testSubject.getStateAt( 0 ).name, "state006" );
			Assert.assertEquals( testSubject.getStateAt( 1 ).name, "state005" );
			Assert.assertEquals( testSubject.getStateAt( 2 ).name, "state004" );
			Assert.assertEquals( testSubject.getStateAt( 3 ).name, "state003" );
			Assert.assertEquals( testSubject.getStateAt( 4 ).name, "state002" );
			Assert.assertNull( testSubject.getStateAt( 5 ) );
		}
		
		[Test]
		public function testGetReferringActionAt():void
		{
			Assert.assertNull( testSubject.getReferringActionAt( -1 ) );
			Assert.assertEquals( testSubject.getReferringActionAt( 0 ), "action006" );
			Assert.assertEquals( testSubject.getReferringActionAt( 1 ), "action005" );
			Assert.assertEquals( testSubject.getReferringActionAt( 2 ), "action004" );
			Assert.assertEquals( testSubject.getReferringActionAt( 3 ), "action003" );
			Assert.assertEquals( testSubject.getReferringActionAt( 4 ), "action002" );
			Assert.assertNull( testSubject.getReferringActionAt( 5 ) );
		}
		
		[Test]
		public function testGet_historyLength():void
		{
			Assert.assertEquals( testSubject.historyLength, 5 );
		}
		
		[Test]
		public function testSet_historyLength():void
		{
			testSubject.historyLength = 3;
			Assert.assertEquals( testSubject.getStateAt( 0 ).name, "state006" );
			Assert.assertEquals( testSubject.getStateAt( 1 ).name, "state005" );
			Assert.assertEquals( testSubject.getStateAt( 2 ).name, "state004" );
			Assert.assertNull( testSubject.getStateAt( 3 ) );
			
			testSubject.historyLength = 5;
			
			history.setTransition( "action007" );
			history.confirmTransitionToNewState( new State("state007") );
			history.setTransition( "action008" );
			history.confirmTransitionToNewState( new State("state008") );
			history.setTransition( "action009" );
			history.confirmTransitionToNewState( new State("state009") );
			
			Assert.assertEquals( testSubject.getStateAt( 0 ).name, "state009" );
			Assert.assertEquals( testSubject.getStateAt( 1 ).name, "state008" );
			Assert.assertEquals( testSubject.getStateAt( 2 ).name, "state007" );
			Assert.assertEquals( testSubject.getStateAt( 3 ).name, "state006" );
			Assert.assertEquals( testSubject.getStateAt( 4 ).name, "state005" );
			Assert.assertNull( testSubject.getStateAt( 5 ) );
		}
		
		[Test]
		public function testGet_isInTransition():void
		{
			Assert.assertFalse( testSubject.isInTransition  );
			history.setTransition( "action010" );
			Assert.assertTrue( testSubject.isInTransition  );
			history.confirmTransitionToNewState( new State("state010") );
			Assert.assertFalse( testSubject.isInTransition  );
			history.setTransition( "action011" );
			Assert.assertTrue( testSubject.isInTransition  );
			history.cancelTransition();
			Assert.assertFalse( testSubject.isInTransition  );
		}
		
		[Test]
		public function testGet_previousState():void
		{
			Assert.assertEquals( testSubject.previousState.name, "state005");
		}
	}
}